package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.SkusBean;

import org.xutils.x;

import java.util.List;

/**
 * Created by admin on 2016/12/6.
 */
public class HomePageGridAdapter extends BaseAdapter {
    private Context context;
    private List<SkusBean> skus;
    private View.OnClickListener listener;

    private boolean isShow;

    public void setShow(boolean show) {
        isShow = show;
    }

    private int index;


    public HomePageGridAdapter(Context context, List<SkusBean> skus, View.OnClickListener listener, int index) {
        this.context = context;
        this.skus = skus;
        this.listener = listener;
        this.index = index;
    }

    @Override
    public int getCount() {
        if (isShow) {
            return skus.size();
        } else
            return skus.size() > 6 ? 6 : skus.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {

        return position >= 5 ? 1 : 2;
    }

    /*@Override
    public int getViewTypeCount() {
        return 2;
    }*/

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HomePageGridViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new HomePageGridViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.homepage_grid_item, null);
            viewHolder.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
            viewHolder.rl_more = (RelativeLayout) convertView.findViewById(R.id.rl_more);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (HomePageGridViewHolder) convertView.getTag();
        }

        switch (getItemViewType(position)) {
            case 1:
                if (isShow)
                    viewHolder.rl_more.setVisibility(View.GONE);
                else
                    viewHolder.rl_more.setVisibility(View.VISIBLE);
                break;
            case 2:
                try {
                    viewHolder.rl_more.setVisibility(View.GONE);
                } catch (Exception e) {

                }
                break;
            default:
                break;

        }
        x.image().bind(viewHolder.iv_img, skus.get(position).getImg());
        viewHolder.rl_more.setOnClickListener(listener);
        viewHolder.rl_more.setTag(index);
        return convertView;
    }

    class HomePageGridViewHolder {
        ImageView iv_img;
        RelativeLayout rl_more;
    }
}
